<?php

namespace Admin\Controller;

use Think\Controller;

class IndexController extends BaseController
{

    public function index()
    {

        $info = session('adminster');
        if ($info['role_id'] != 0) {
            $userinfo = M('admin')->where("id = {$info['id']}")->find();
        } elseif (!$info) {
            redirect('/index.php?m=Admin&c=Login&a=index');
        }

        $this->assign('info', $info);
        $this->assign('userinfo', $userinfo);
        $this->display();
    }

    public function main()
    {

        $admin = session('adminster');
        if ($admin['role_id'] != 0) {
            $map['t_id'] = $admin['id'];
        }

        $this->display();
    }

    public function pwd()
    {

        $user2 = session('adminster');
        if ($user2['role_id'] != 0) {
            $User = M('agent');
        } else {
            $User = M('admin');
        }
        if ($_POST) {

            if (!IS_AJAX) {
                $this->error('提交方式不正确', U('index/pwd'), 0);
            } else {
                $data['username'] = I('post.user');
                $data['password'] = md5(I('post.oldpassword'));
                $newpassword = md5(I('post.newpassword'));
                $repassword = md5(I('post.repassword'));

                $result = $User->where($data)->find();

                if ($result) {
                    if ($newpassword != $repassword) {
                        $this->error("两次输入新密码不一致");
                    } else {
                        $User->where($data)->setField('password', $newpassword);
                        $this->success("修改成功", U('Login/index'), 1);
                    }
                } else {
                    $this->error("账号或密码不正确");
                }
            }
        }
        $this->assign('user2', $user2);
        $this->display();
    }

    public function del()
    {

        delFileByDir(APP_PATH . 'Runtime/');
        $this->success('删除缓存成功！', U('Admin/Index/index'));
    }


}